/**
 * Created by Administrator on 2016/8/1.
 */

var uuid = require('node-uuid');
var async = require('async');
var cartDao = require('../dao/cartDao');

//确认订单页面
exports.comfirmorder = function (userid, callback) {
    cartDao.comfirmorder(userid, function (err, data) {
        callback(err, data);
    })
};

//确认订单定金待支付数据
exports.advancewait = function (reservearray, callback) {
    var payarr = [];
    var index = [];
    for (var j = 0; j < reservearray.length; j++) {
        var id = reservearray[j].id;
        index.push(id);
    }
    cartDao.clearcart(index, function (err, data2) {
        var payid = uuid.unparse(uuid.v4(null, new Buffer(32), 0));
        for (var i = 0; i < reservearray.length; i++) {
            if(!reservearray[i].gooddetailid){
                reservearray[i].gooddetailid = reservearray[i].id;
            }
            reservearray[i].id = uuid.unparse(uuid.v4(null, new Buffer(32), 0));
            reservearray[i].orderstatus = 4;
            reservearray[i].status = 1;
            reservearray[i].starttime = new Date(reservearray[i].starttime);
            reservearray[i].endtime = new Date(reservearray[i].endtime);
            reservearray[i].createAt = new Date();
            reservearray[i].payid = payid;
            if (reservearray[i].deposit) {
                delete(reservearray[i]["deposit"]);
            }
        }
        cartDao.advancewait(reservearray, function (err, data) {
            callback(err, data);
        })
    })
};


//确认订单已支付定金
exports.payearnest = function (params, params2,callback) {
    cartDao.payearnest(params, function (err, rows1) {
        cartDao.addpay(params, function (err, rows2) {
            cartDao.contact(params, function (err, rows3) {
                cartDao.addpayment(params2, function (err, rows4) {
                    callback(err, {rows1: rows1, rows3: rows3});
                })
            })
        })
    })
};

//购物车
exports.shoppingcart = function (userid, callback) {
    cartDao.shoppingcart(userid, function (err, data) {
        callback(err, data);
    })
};

